package com.tinygray.cas;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @Author: tinygray
 * @Description: 公众号:Madison龙少，关注我你会越来越优秀。
 * @className: AtomicReferenceDemo
 * @create: 2021-05-06 23:40
 */
public class AtomicReferenceDemo {
    public static void main(String[] args) {
        User qlh = new User("qlh", 24);
        User qqq = new User("qqq", 22);
        //创建原子引用包装类
        AtomicReference<User> atomicReference = new AtomicReference<User>();
        //现在著物理内存是qlh
        atomicReference.set(qlh);
        //比较并交换 期望值qlh 与主物理内存值相等，那么就交换成qqq
        System.out.println(atomicReference.compareAndSet(qlh, qqq) + "\t" + atomicReference.get().toString());
        //比较并交换 期望值qlh  与主物理内存qqq不相等  交换失败
        System.out.println(atomicReference.compareAndSet(qlh, qqq) + "\t" + atomicReference.get().toString());
    }
}
@Data
@AllArgsConstructor
class User {
    private String name;
    private int age;
}
